<template>
  <el-dialog title="售后工单选择"
             v-if="showFlag"
             :visible.sync="showFlag"
             :modal=false
             width="80%"
             center
  >
    <el-card style="border-radius: 10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工单编号" prop="workOrderCode">
        <el-input
          v-model="queryParams.workOrderCode"
          placeholder="请输入工单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="工单类型" prop="workOrderType">
        <el-input
          v-model="queryParams.workOrderType"
          placeholder="请输入工单类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="客户ID" prop="clientId">
        <el-input
          v-model="queryParams.clientId"
          placeholder="请输入客户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="客户编码" prop="clientCode">
        <el-input
          v-model="queryParams.clientCode"
          placeholder="请输入客户编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="clientName">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-card style="border-radius: 10px">
    <el-table v-loading="loading" :data="afterSalesWorkOrderList" @current-change="handleCurrent" @row-dblclick="handleRowDbClick" border>
<!--      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />-->
      <el-table-column width="50" align="center" >
        <template v-slot="scope">
          <el-radio v-model="selectedRepairId" :label="scope.row.id" @change="handleRowChange(scope.row)">{{""}}</el-radio>
        </template>
      </el-table-column>
      <el-table-column label="工单编号" align="center" prop="workOrderCode">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['as:afterSalesWorkOrder:query']"
          >{{scope.row.workOrderCode}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="工单类型" align="center" prop="workOrderType" />
<!--      <el-table-column label="客户ID" align="center" prop="clientId" />-->
<!--      <el-table-column label="客户编码" align="center" prop="clientCode" />-->
      <el-table-column label="客户名称" align="center" prop="clientName" />
<!--      <el-table-column label="客户地址" align="center" prop="clientAddress" />-->
<!--      <el-table-column label="客户简称" align="center" prop="clientNick" />-->
<!--      <el-table-column label="问题分类" align="center" prop="issueType" />-->
<!--      <el-table-column label="状态" align="center" prop="workOrderStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_order_status" :value="scope.row.workOrderStatus"/>
        </template>
      </el-table-column>-->
      <el-table-column label="联系人" align="center" prop="person" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />


    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  listAfterSalesWorkOrder,
  getAfterSalesWorkOrder,
  delAfterSalesWorkOrder,
  addAfterSalesWorkOrder,
  updateAfterSalesWorkOrder
} from "@/api/mes/as/afterSalesWorkOrder";
import {genCode} from "@/api/system/autocode/rule";
import ClientSelect from "@/components/clientSelect/single.vue";
import AfterSalesWorkOrderLine from "../line.vue"

export default {
  name: "AfterSalesWorkOrder",
  dicts: ['mes_order_status'],
  components: {
    ClientSelect,
    AfterSalesWorkOrderLine
  },
  data() {
    return {
      selectedRows: undefined,
      showFlag:false,
      selectedRepairId: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 售后工单表格数据
      afterSalesWorkOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workOrderCode: null,
        workOrderType: null,
        clientId: null,
        clientCode: null,
        clientName: null,
        clientAddress: null,
        clientNick: null,
        issueType: null,
        workOrderStatus: null,
        person: null,
        phone: null,
        price: null,
        url: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //自动生成编码
      autoGenFlag:false,
      // 标识
      optType: undefined,
      // 上传文件类型
      fileType: [
        // 文档类
        "pdf", "doc", "docx", "xls", "xlsx",
        // 图片类
        "jpg", "jpeg", "png", "gif", "bmp",
        // 视频类
        "mp4", "avi", "rmvb", "mkv", "mov",
        // 压缩文件类
        "zip", "rar", "7z", "tar", "gz"
      ],
      // 上传文件列表
      fileList: [],
      options: [
        { "label": "产品质量问题", "value": "quality_issue" },
        { "label": "物流运输问题", "value": "logistics_issue" },
        { "label": "安装调试问题", "value": "installation_issue" },
        { "label": "使用操作问题", "value": "operation_issue" },
        { "label": "退换货申请", "value": "return_request" },
        { "label": "维修服务请求", "value": "repair_service" },
        { "label": "发票与付款问题", "value": "invoice_payment_issue" },
        { "label": "保修期咨询", "value": "warranty_inquiry" },
        { "label": "投诉建议", "value": "complaint_suggestion" },
        { "label": "其他问题", "value": "other_issue" }
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询售后工单列表 */
    getList() {
      this.selectedRows=[]
      this.loading = true;
      listAfterSalesWorkOrder(this.queryParams).then(response => {
        this.afterSalesWorkOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleCurrent(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    handleRowDbClick(row){
      if(row){
        this.selectedRows = row;
        this.$emit('onSelected',this.selectedRows);
        this.showFlag = false;
        this.selectedRows=[]
      }
    },
    handleRowChange(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    //确定选中
    confirmSelect() {
      if (this.selectedRepairId == null || this.selectedRepairId == 0) {
        this.$notify({
          title: '提示',
          type: 'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.$emit('onSelected', this.selectedRows);
      this.showFlag = false;
      this.selectedRows=[]
    },
    // 取消按钮
    cancel() {
      this.showFlag = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        workOrderCode: null,
        workOrderType: '售后工单',
        clientId: null,
        clientCode: null,
        clientName: null,
        clientAddress: null,
        clientNick: null,
        issueType: null,
        workOrderStatus: "PREPARE",
        person: null,
        phone: null,
        price: null,
        remark: null,
        url: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.autoGenFlag = false;
      this.fileList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加售后工单";
      this.optType = 'add'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAfterSalesWorkOrder(id).then(response => {
        this.form = response.data;
        response.data.url === null ? this.fileList = [] : this.fileList = response.data.url.split(',');
        this.open = true;
        this.title = "修改售后工单";
        this.optType = 'update'
      });
    },
    /** 详情按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id || this.ids;
      getAfterSalesWorkOrder(id).then(response => {
        this.form = response.data;
        response.data.url === null ? this.fileList = [] : this.fileList = response.data.url.split(',');
        this.open = true;
        this.title = "查看售后工单";
        this.optType = 'view'
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAfterSalesWorkOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAfterSalesWorkOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleSubmit() {
      this.form.workOrderStatus = 'FINISHED';
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateAfterSalesWorkOrder(this.form).then(response => {
            this.$modal.msgSuccess("提交成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除售后工单编号为"' + ids + '"的数据项？').then(function() {
        return delAfterSalesWorkOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('as/afterSalesWorkOrder/export', {
        ...this.queryParams
      }, `afterSalesWorkOrder_${new Date().getTime()}.xlsx`)
    },
    //自动生成编码
    handleAutoGenChange(autoGenFlag){
      if(autoGenFlag){
        genCode('AFTER_SALES_WORK_ORDER_CODE').then(response =>{
          this.form.workOrderCode = response;
        });
      }else{
        this.form.workOrderCode = null;
      }
    },
    handleSelectClient(){
      this.$refs.clientSelect.showFlag = true;
    },
    //客户选择弹出框
    onClientSelected(obj){
      if(obj !== undefined && obj !== null){
        this.form.clientId = obj.clientId;
        this.form.clientCode = obj.clientCode;
        this.form.clientName = obj.clientName;
        this.form.clientAddress = obj.address;
      }
    },
    // 上传成功回调
    handleUploaded(data) {
      this.form.url = data;
    },
    // 删除成功回调
    handleRemoved(data) {
      this.form.url = data;
    },
  }
};
</script>
